<?php

namespace App\Services\Common;


use http\Client;
use Illuminate\Support\Facades\Log;

class Ding
{
    const BASE_URI = "https://oapi.dingtalk.com/";
    const NOTICE_URL = 'robot/send?access_token=21498bb8e91172a75b22a329b21a46ccf6bd2009462892f0a7cf2c7817c94408';

    /**
     * Description: 钉钉文本通知
     * @param        $data
     * @param array $at
     * @param bool $isAtAll
     * @throws GuzzleException
     */
    public  function text($data, $at = [], $isAtAll = false)
    {
        $notice_url = self::NOTICE_URL;

        $data = "#### 邮件异常--触发时间" . date('Y-m-d H:i:s') . "\n" . $data;

        $pushData = [
            'msgtype' => 'text',
            'text' => ['content' => $data],
            'at' => ['atMobiles' => $at, 'isAtAll' => $isAtAll],
        ];
        self::getData($notice_url, $pushData);
    }

    /**
     * @Notes:asin添加成功通知
     * @Interface asinNotice
     * @param $data
     * @param $at
     * @param $isAtAll
     * @author: wuyu
     * @Time: 2023/7/12   17:01
     */
    public  function asinNotice($data, $at = [], $isAtAll = false)
    {
        $notice_url = self::NOTICE_URL;

        $data = "#### asin:".$data . '评价监控--需要添加评论监控' . date('Y-m-d H:i:s');

        $pushData = [
            'msgtype' => 'text',
            'text' => ['content' => $data],
            'at' => ['atMobiles' => $at, 'isAtAll' => $isAtAll],
        ];
        self::getData($notice_url, $pushData);
    }

    /**
     * Description: 钉钉预警markdown
     * @param        $data
     * @param array $at
     * @param bool $isAtAll
     * @throws GuzzleException
     */
    public static function markdown($data, $at = [], $isAtAll = false)
    {
        $notice_url = self::NOTICE_URL[rand(0, count(self::NOTICE_URL) - 1)];
        $data['text'] = "#### 异常触发时间" . date('Y-m-d H:i:s') . "\n" . $data['text'];
        $pushData = [
            'msgtype' => 'markdown',
            'markdown' => $data,
            'at' => ['atMobiles' => $at, 'isAtAll' => $isAtAll],
        ];
        self::getData($notice_url, $pushData);
    }

    /**
     * Description: 钉钉预警处理
     * @param $notice_url
     * @param $pushData
     * @throws GuzzleException
     */
    public static function getData($notice_url, $pushData)
    {
        try {
            $client = new \GuzzleHttp\Client(['base_uri' => self::BASE_URI, 'timeout' => 3.0]);
            $res = $client->request('POST',
                $notice_url,
                ['headers' => ['Content-Type' => 'application/json;charset=utf-8',],
                    'json' => $pushData,
                    'verify' => false]);
            $result = json_decode($res->getBody()->getContents(), true);
            if ($result['errmsg'] != 'ok' && $result['errcode'] != 0) {
                Log::channel('dingLog')->debug('钉钉推送数据失败:'.$result['errmsg'],['result' => $result, 'url' => $notice_url]);
            }
        } catch (\Exception $e) {
            Log::channel('dingLog')->debug('钉钉推送数据失败:'.$e->getMessage().' '.$e->getFile().' '.$e->getLine(),['data' => $pushData, 'url' => $notice_url]);
        }
    }
}
